import 'package:get/get.dart';
import 'package:xmshop/app/services/storage.dart';
import '../../../services/searchService.dart';

class MySearchController extends GetxController {
  String keyword = '';
  RxList historyList = [].obs;
  @override
  void onInit() {
    super.onInit();
    getHistoryData();
  }

  @override
  void onClose() {
    super.onClose();
  }

  getHistoryData() async {
    var tempList = await SearchService.getHistoryData();
    if (tempList.isNotEmpty) {
      historyList.addAll(tempList);
      update();
    }
  }

  clearHistoryData() async {
    await SearchService.clearHistoryData();
    historyList.clear();
    update();
  }

  removeHistoryData(keyword) async {
    var tempList = await SearchService.getHistoryData();
    if (tempList.isNotEmpty) {
      tempList.remove(keyword);
      await Storage.setData("searchList", tempList);
      // 注意
      historyList.remove(keyword);
      update();
    }
  }
}
